package com.example.komunikator;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import android.app.Dialog;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddUserToSQL extends Dialog implements android.view.View.OnClickListener{
	private SingleChatWindow singleChatWindow;
	
	
	public AddUserToSQL(SingleChatWindow singleChatWindow){
		super(singleChatWindow);
		this.singleChatWindow = singleChatWindow;
		
	}

protected void onStart(){
	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
	StrictMode.setThreadPolicy(policy);
	
	super.onStart();
	setContentView(R.layout.add_new_user);
	
	getWindow().setFlags(4,4);
	setTitle("Add New User");
	Button ok = (Button) findViewById(R.id.userokSettingButton);
	ok.setOnClickListener(this);
	
}
	 
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String user = getText(R.id.user_name);
		
		try {
			Connection connection = SQLLogic.getInstance().getConnection();
			Statement st = connection.createStatement();
			int room_id=1;
			String query="INSERT INTO Users Values('','"+user+"','"+room_id+"')";
			Log.i("SQLDebug", query);
			st.executeUpdate(query);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		dismiss();
	}
	
    private String getText(int id) {
        EditText widget = (EditText) this.findViewById(id);
        return widget.getText().toString();
    }
}
